package com.zte.cl.thread;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;

import com.zte.cl.model.CompanyBaseInfo;
import com.zte.cl.repository.CompanyBaseInfoRepository;
import com.zte.cl.repository.UserInfoRepository;
import com.zte.cl.utils.AESEncrypt;
import com.zte.cl.utils.ConstantUtil;
import com.zte.cl.utils.HttpUtil;
import com.zte.cl.utils.SpringContextUtil;
import com.zte.cl.utils.UuidUtil;

public class PushMsgThread implements Runnable {

	private static final Log log = LogFactory.getLog(PushMsgThread.class);
	
	private String publicAccount;
	private List<String> companyIdList;
	private Map<String, String> msg;
	
	
	private UserInfoRepository userInfoRepository;
	private CompanyBaseInfoRepository companyBaseInfoRepository;
	
	public PushMsgThread(String publicAccount, List<String> companyIdList, Map<String, String> msg) {
		this.publicAccount = publicAccount;
		this.companyIdList = companyIdList;
		this.msg = msg;
		this.userInfoRepository = (UserInfoRepository) SpringContextUtil.getBean("userInfoRepository");
		this.companyBaseInfoRepository = (CompanyBaseInfoRepository) SpringContextUtil.getBean("companyBaseInfoRepository");
	}
	
	@Override
	public void run() {
		for(String companyId : companyIdList) {
			CompanyBaseInfo companyBaseInfo = companyBaseInfoRepository.findByCompanyid(companyId);
			String[] receivers = userInfoRepository.findPidByCompanyid(companyId);
			if(receivers.length > 0) {
				Integer currentCount = 1;
				int sleepTime = ConstantUtil.NEWS_PUSH_THREAD_ERROR_SLEEP_TIME;
				
				while(true) {
					if(currentCount > ConstantUtil.NEWS_PUSH_THREAD_ERROR_ATTEMPT_COUNT) {
						log.warn("已经达到或超过最大尝试次数"+ConstantUtil.NEWS_PUSH_THREAD_ERROR_ATTEMPT_COUNT+", 不再尝试!companyId:" + companyId);
						log.error("推送新闻失败! companyId:" + companyId);
						break;
					}
					
					try {
						if(currentCount > 1) {
							log.warn("休眠" + (sleepTime/1000) + "秒之后再次尝试");
							Thread.sleep(sleepTime);
						}
					
						log.info("第" + currentCount + "次推送新闻, companyId:" + companyId);
						if(pushMsg(companyBaseInfo, receivers)) {
							break;
						}
					} catch (Exception e) {
						log.error("PushMsgThread执行错误, companyId:" + companyId, e);
					} finally {
						currentCount++;
					}
				}
			} else {
				log.warn("没有获取到用户pid列表,companyId:" + companyId);
				continue;
			}
		}
	}
	
	private boolean pushMsg(CompanyBaseInfo companyBaseInfo, String[] receivers) throws Exception {
		JSONObject json = new JSONObject();
		String companyId = companyBaseInfo.getCompanyid();
		json.put("spID", companyId);
		json.put("spPwd", companyBaseInfo.getCompanypassword());
		json.put("senderNo", publicAccount);
		json.put("receivers", receivers);
		json.put("msgID", UuidUtil.getShortUuid());
		json.put("msgTitle", msg.get("title"));
		json.put("msgContent", msg.get("content"));
		json.put("terminalType", "3");
		
		String jsonStr = AESEncrypt.enCrypt("", json.toString());
		String respStr = HttpUtil.sendPost(ConstantUtil.NEWS_PUSH_INTERFACE_URL , jsonStr, ConstantUtil.IF_PROXY);
		respStr = AESEncrypt.deCrypt("", respStr);
		log.info("推送新闻respStr = " + respStr);
		
		JSONObject respJson = new JSONObject(respStr);
		if(respJson.getString("result").equals("0")) {
			log.info("推送新闻成功, companyId:" + companyId);
			return true;
		} else {
			log.warn("推送新闻返回错误的结果!companyId:" + companyId);
			return false;
		}
	}
	
}
